<template>
  <div class="login-wrapper">
    <div class="login-left">
      <div class="login-left_logo">
        <img style="width: 300px" src="@/assets/images/logo.png" alt="" />
      </div>
      <!-- <div class="login-left_title">
        <p>欢迎来到，</p>
        <p>智能充电管理平台</p>
        <p>Welcome to the Management Platform Of ANDPOWER</p>
      </div> -->
      <!-- <div class="login-left_footer">
        Copright&copy;{{
          currentYear
        }}&nbsp;中移物联网&nbsp;版权所有&nbsp;|&nbsp;<a
          href="https://beian.miit.gov.cn/"
          target="_blank"
          >渝ICP备13005647号-{{ copyrightNumber }}</a
        >&nbsp;|&nbsp;版本号{{ version }}
      </div> -->
    </div>
    <div class="login-right">
      <i-form
        ref="loginForm"
        class="login-form"
        label-position="top"
        :model="loginFormData"
        :items="loginFormItems"
        :rules="loginRules"
      >
        <div class="login-title">欢迎来到智能充电管理后台</div>
        <template slot="code">
          <div
            style="
              display: flex;
              align-items: center;
              padding: 0 4px;
              justify-content: space-between;
            "
          >
            <span
              style="
                font-weight: 700;
                color: #9b9b9b;
                line-height: 30px;
                font-size: 14px;
              "
              >验证码</span
            >
            <span class="create-code" @click="createCode">换一张</span>
          </div>
          <el-form-item prop="code">
            <div class="autcode-content">
              <el-input
                v-model="loginFormData.imgCode"
                class="authCode"
                :maxlength="4"
              />
              <div class="login-right_code">
                <img class="code" :src="authCodeSrc" />
              </div>
            </div>
          </el-form-item>
        </template>
        <template slot="submit">
          <div class="submit-area">
            <div @click="handleClickForget" class="submit-area-forgetpwd">
              忘记密码?
            </div>
            <el-button
              class="submit-button"
              :loading="loading"
              @click="submitForm"
              type="primary"
            >
              登录
            </el-button>
          </div>
        </template>
        <!-- <template slot="agentApply">
          <div class="apply-area">
            <el-button class="apply-button" @click="agentApply">
              申请账号
            </el-button>
          </div>
        </template> -->
      </i-form>
    </div>
    <ForgetPwd v-if="visible" :visible.sync="visible" />
  </div>
</template>

<script>
import md5 from 'md5'
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min.js'
import { login, loginFourWheel } from '@/api/login'
import { getWebLoginImg, getFourWheelWebLoginImg } from '@/api/common'
import { appConfig } from '@/config'
import ForgetPwd from './components/ForgetPwd'

export default {
  name: 'Login',
  components: { ForgetPwd },
  data() {
    return {
      currentYear: new Date().getFullYear(),
      version: appConfig.version,
      copyrightNumber: process.env.VUE_APP_COPYRIGHT_NUMBER,
      authCodeSrc: '',
      loginFormData: {
        account: '',
        password: '',
        // signOnPlatform: localStorage.getItem('PLATFORM_TYPE') || 'twoWheel',
        signOnPlatform: 'twoWheel',
      },
      loginRules: {
        account: [
          {
            emptymsg: '请输入账号',
            required: true,
            trigger: 'blur',
            message: '请输入账号',
          },
        ],
        password: [
          {
            emptymsg: '请输入密码',
            required: true,
            trigger: 'blur',
            message: '请输入密码',
          },
        ],
        imgCode: [
          {
            emptymsg: '请输入验证码',
            required: true,
            trigger: 'blur',
            message: '请输入验证码',
          },
        ],
      },
      loginFormItems: [
        // {
        //   type: 'radio',
        //   value: 'signOnPlatform',
        //   options: [
        //     {
        //       label: 'twoWheel',
        //       text: '电动自行车充电运营平台',
        //     },
        //     {
        //       label: 'fourWheel',
        //       text: '汽车充电运营平台',
        //     },
        //   ],
        //   change: this.handleChangeType,
        // },
        {
          type: 'text',
          value: 'account',
          label: '账号',
          placeholder: '请输入账号',
          clearable: true,
        },
        {
          type: 'password',
          value: 'password',
          label: '密码',
          placeholder: '请输入账号密码',
          clearable: true,
          showPassword: true,
        },
        {
          slot: 'code',
        },
        {
          slot: 'submit',
        },
        {
          // type: 'slot',
          // value: 'agentApply',
          slot: 'agentApply',
        },
      ],
      time: '',
      loading: false,
      visible: false,
    }
  },
  created() {
    this.createCode()
  },
  methods: {
    handleClickForget() {
      this.visible = true
    },
    handleChangeType(val) {
      localStorage.setItem('PLATFORM_TYPE', val)
      this.createCode()
    },
    createCode() {
      this.time = new Date().getTime()
      const authCodeApi =
        process.env.VUE_APP_BASE_URL_API +
        '/open/getSystemUserLoginCerificationImg'
      this.authCodeSrc = `${authCodeApi}?uuid=${this.time}`
    },
    submitForm() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          let encryptor = new JSEncrypt()
          encryptor.setPublicKey(this.publicKey)
          const data = {
            ...this.loginFormData,
            // password: encryptor.encrypt(md5(this.loginFormData.password)),
            password: md5(this.loginFormData.password),
          }
          const handler =
            data.signOnPlatform === 'twoWheel' ? login : loginFourWheel
          handler(data).then(
            (res) => {
              this.$store.dispatch('fedLogOut').then(() => {
                this.loading = false
                let tokenName =
                  data.signOnPlatform === 'twoWheel' ? 'LG_TK' : 'LG_FW_TK'
                localStorage.setItem(tokenName, res)
                // //  本地启动四轮的临时token
                // localStorage.setItem(tokenName, '4284d07b15d549d9b1e0463f821763ac')
                localStorage.setItem('LG_MP', md5(this.loginFormData.password))
                if (data.signOnPlatform === 'twoWheel') {
                  this.$router.push('/dashboard')
                } else {
                  window.location.href = `${process.env.VUE_APP_TWO_WHEEL_JUMP_FOUR_WHEEL}/fourWheel/#/dashboard`
                }
              })
            },
            () => {
              this.loading = false
              this.createCode()
            }
          )
        }
      })
    },
    agentApply() {
      this.$router.push('/agentApply')
    },
  },
}
</script>
<style lang="scss" scoped>
.login-wrapper {
  width: 100%;
  height: 100vh;
  display: flex;
}
.login-left {
  background: url(../../assets/images/back-login.png) no-repeat center center;
  background-size: 100% 100%;
  flex: 4;
  height: 100%;
  position: relative;
  .login-left_logo {
    margin: 40px auto auto 55px;
    img {
      vertical-align: middle;
    }
    img:first-child {
      width: 106px;
      margin-right: 30px;
    }
    img:last-child {
      width: 103px;
    }
  }
  .login-left_title {
    font-size: 56px;
    color: #fff;
    width: 648px;
    position: absolute;
    left: 50%;
    margin-left: -350px;
    top: 25%;
    p {
      margin: 0;
      &:first-child {
        margin-bottom: 43px;
      }
      &:last-child {
        font-size: 24px;
        word-spacing: 10px;
        margin-top: 61px;
      }
    }
  }
  .login-left_footer {
    color: rgba(114, 114, 114, 1);
    font-size: 14px;
    position: absolute;
    text-align: center;
    bottom: 14px;
    width: 100%;
  }
}
.login-right {
  flex: 2;
  position: relative;
  .login-right_code {
    position: absolute;
    top: -2px;
    right: 0;
  }
}
.login-form {
  min-width: 300px;
  margin: 25% 110px 0;
  & ::v-deep .el-form-item {
    margin-bottom: 40px;
  }
  & ::v-deep .el-form-item__label {
    color: #9b9b9b;
    line-height: 30px;
    padding: 0;
    &::before {
      content: '' !important;
    }
  }
  .login-title {
    color: #333333;
    font-size: 28px;
    margin-bottom: 30px;
    margin-left: 1px;
    text-align: center;
    line-height: 40px;
  }
  .authCode {
    vertical-align: middle;
  }
  .code {
    margin-right: 10px;
    vertical-align: middle;
  }
  .remember-userInfo {
    font-weight: 400;
  }

  .apply-area {
    padding-top: 20px;
  }

  .submit-area {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .submit-area-forgetpwd {
      cursor: pointer;
      font-weight: bold;
    }
  }
  .submit-button {
    width: 120px;
    height: 40px;
    color: #fff;
    font-size: 16px;
  }
  .apply-button {
    border: 1px solid #7c94fa;
    border-radius: 4px;
    color: #4c71f9;
    font-size: 12px;
    height: 32px;
    line-height: 8px;
  }
  .create-code {
    cursor: pointer;
    color: #666;
  }
}
</style>
